package tech.heptagram.staff.tools.service;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.core.common.rest.RestResultEnum;
import tech.heptagram.staff.exception.StaffApiException;
import tech.heptagram.staff.model.ProcessRecord;
import tech.heptagram.staff.query.ProcessRecordQuery;
import tech.heptagram.staff.query.RejectMsgReq;
import tech.heptagram.staff.service.ProcessRecordService;

/**
 * @author wang.yuchuan
 * @createdAt 2019/3/22
 */
@Slf4j
public class ProcessRecordServiceRestTemplateImpl extends AbstractServiceImpl implements ProcessRecordService {

    private RestTemplate restTemplate;

    public ProcessRecordServiceRestTemplateImpl(String staffApiDomain, RestTemplate restTemplate) {
        super(staffApiDomain);
        this.restTemplate = restTemplate;
    }

    @Override
    public void save(ProcessRecord vo) {

        log.info("请求 保存审批单 信息接口save: {}", vo);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/save";

        RequestEntity<ProcessRecord> requestEntity = RequestEntity.post(URI.create(url))
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .body(vo);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 保存审批单 信息接口save:  返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public void updateStateToStart(String bpmsProcessId) {

        log.info("请求 修改审批id为{}的申请状态到成功 接口", bpmsProcessId);
        ProcessRecord record = new ProcessRecord();
        record.setBpmsProcessId(bpmsProcessId);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/update/state/start";

        RequestEntity<ProcessRecord> requestEntity = RequestEntity.post(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(record);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 修改审批的申请状态到成功 接口：返回结果：{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public void updateStateToTerminate(String bpmsProcessId) {
        log.info("请求 修改审批id为{}的申请状态到撤销 接口", bpmsProcessId);
        ProcessRecord record = new ProcessRecord();
        record.setBpmsProcessId(bpmsProcessId);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/update/state/terminate";

        RequestEntity<ProcessRecord> requestEntity = RequestEntity.post(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(record);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 修改审批的申请状态到撤销 接口：返回结果：{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public void updateStateToFinish(String bpmsProcessId, Integer applicationResult) {
        log.info("请求 修改审批id为{}的申请状态到完成 接口", bpmsProcessId);
        ProcessRecord record = new ProcessRecord();
        record.setBpmsProcessId(bpmsProcessId);
        record.setApplicationResult(applicationResult);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/update/state/finish";

        RequestEntity<ProcessRecord> requestEntity = RequestEntity.post(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(record);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 修改审批的申请状态到完成 接口：返回结果：{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public void updateRejectMessage(String bpmsProcessId, String rejectMessage) {
        log.info("请求 修改id为{}的审批申请拒绝消息 接口", bpmsProcessId);
        RejectMsgReq msgReq = new RejectMsgReq();
        msgReq.setBpmsProcessId(bpmsProcessId);
        msgReq.setRejectMessage(rejectMessage);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/updateRejectMsg";

        RequestEntity<RejectMsgReq> requestEntity = RequestEntity.post(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(msgReq);

        ParameterizedTypeReference<ResponseResult> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult>() {
        };
        ResponseEntity<ResponseResult> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 修改审批申请拒绝消息 接口：返回结果：{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
    }

    @Override
    public ProcessRecord findByBpmsProcessId(String bpmsProcessId) {
        log.info("请求 根据审批ID{}获取审批信息 接口", bpmsProcessId);
        ProcessRecord record = new ProcessRecord();
        record.setBpmsProcessId(bpmsProcessId);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/findProcessRecord";

        RequestEntity<ProcessRecord> requestEntity = RequestEntity.post(URI.create(url))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .body(record);

        ParameterizedTypeReference<ResponseResult<ProcessRecord>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<ProcessRecord>>() {
        };
        ResponseEntity<ResponseResult<ProcessRecord>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求 根据审批ID获取审批信息 接口：返回结果：{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }

    @Override
    public List<ProcessRecord> findByUserProcessTypeAndState(String staffCode, Integer processType, Integer processState) {

        ProcessRecordQuery query = new ProcessRecordQuery();
        query.setStaffCode(staffCode);
        query.setProcessType(processType);
        query.setProcessState(processState);


        log.info("请求根据查询条件获取 审批纪录 信息接口findByQuery: {}", query);

        String url = getStaffApiDomainUrl() + "/api/v3/bpms-record/find";

        RequestEntity<ProcessRecordQuery> requestEntity = RequestEntity.post(URI.create(url))
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .body(query);

        ParameterizedTypeReference<ResponseResult<List<ProcessRecord>>> responseResultParameterizedTypeReference = new ParameterizedTypeReference<ResponseResult<List<ProcessRecord>>>() {
        };
        ResponseEntity<ResponseResult<List<ProcessRecord>>> resultEntity = restTemplate.exchange(requestEntity, responseResultParameterizedTypeReference);

        log.info("请求根据查询条件获取 审批纪录 信息接口findByQuery:  返回结果:{}", resultEntity.getBody().toString());
        if (resultEntity.getBody().getRetcode() != RestResultEnum.SUCCESS.getKey()) {
            throw new StaffApiException(resultEntity.getBody().getMsg());
        }
        return resultEntity.getBody().getData();
    }
}
